package com.yc.demo.d0316;

import com.yc.demo.util.DBHelper;

import java.sql.Date;

public class BookBiz {

    public void create(String bookname,String bookauthor,String bookpress,String pressdate,String bookimage) throws BizException {


        if (bookname==null ||bookname.trim().isEmpty()){
            throw  new BizException("请输入书名");
        }
        if (bookpress.length()<3){
            throw new BizException("出版社至少三个字符");
        }
        //mysql 默认日期格式 yyyy-mm-dd
        if (!pressdate.matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
            throw new BizException("日期格式不正确");
        }

        String sql = "insert into books (bookname,bookauthor,bookpress,pressdate,bookimage)"+
                " values (?,?,?,?,?)";

        DBHelper.update(sql,bookname,bookauthor,bookpress, Date.valueOf(pressdate),bookimage);
    }

    public void modify(String id, String bookname, String bookauthor, String bookpress, String pressdate, String bookimage,String status) throws BizException {

        if (bookname==null ||bookname.trim().isEmpty()){
            throw  new BizException("请输入书名");
        }
        if (bookpress.length()<3){
            throw new BizException("出版社至少三个字符");
        }
        //mysql 默认日期格式 yyyy-mm-dd
        if (!pressdate.matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
            throw new BizException("日期格式不正确");
        }

        String sql  = "update books set bookname = ? , bookauthor = ? ,bookpress = ?,pressdate = ? ,bookimage = ?,status = ? where id = ? ";
        DBHelper.update(sql,bookname,bookauthor,bookpress, Date.valueOf(pressdate),bookimage,status,id);
    }
    //物理删除
    public void removeT(String id){
        String sql = "delete from books where id = ?";
        DBHelper.update(sql,id);
    }

    //逻辑删除
    public void remove(String id){
        String sql = "update  books set status = 0  where id = ?";
        DBHelper.update(sql,id);
    }
}
